# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=suitesparse
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-suitesparse")
pkgver=5.7.2
pkgrel=1
pkgdesc='A suite of sparse matrix algorithms (mingw-w64)'
url="http://faculty.cse.tamu.edu/davis/suitesparse.html"
license=('GPL')
arch=('any')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/DrTimothyAldenDavis/SuiteSparse/archive/v$pkgver.tar.gz"
        "0001-mingw-w64-Use-a-not-lib-as-AR_TARGET-extension.patch"
        "0002-mingw-w64-Set-SO_OPTS--shared-move-dlls-create-import-libs.patch"
        "0003-mingw-w64-no-CUDA.patch"
        "0004-mingw-w64-install-static-libs.patch"
        "0005-suitesparse-5.4.0-skip-building-Mongoose.patch"
        "0006-suitesparse-5.4.0-skip-building-GraphBLAS.patch")
depends=("${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-metis")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
options=('staticlibs')
sha256sums=('fe3bc7c3bd1efdfa5cffffb5cebf021ff024c83b5daf0ab445429d3d741bd3ad'
            '1c848ba4fd5b07d65e0f8e9f6d5153efc51292d0d487d446afbdacf25e4fd8f7'
            '0f477a8cba3bdff19731560d416dc5f7e072e81ab8837d7f979a4ec89a405266'
            '8a94c54322323773442c2c60f54795d402d64ac126e2a1a67e48a0075f8571fa'
            'ca8aa0a8c3828ae7e2b5ae53c9e5c5c581d613f11763c23d648b38d8c8909485'
            '2ed754de777e4a8d2ebcaf4546f0545be6a87dab4591bc3b6b6798435356411b'
            'f8653b7fa29651b87e17bd398efcd6635cf77ee12a21e316aec291ad778d2a8e')

prepare() {
  cd "${srcdir}"/SuiteSparse-${pkgver}
  patch -p1 -i "${srcdir}"/0001-mingw-w64-Use-a-not-lib-as-AR_TARGET-extension.patch
  patch -p1 -i "${srcdir}"/0002-mingw-w64-Set-SO_OPTS--shared-move-dlls-create-import-libs.patch
  patch -p1 -i "${srcdir}"/0003-mingw-w64-no-CUDA.patch
  patch -p1 -i "${srcdir}"/0004-mingw-w64-install-static-libs.patch
  patch -p1 -i "${srcdir}"/0005-suitesparse-5.4.0-skip-building-Mongoose.patch
  patch -p1 -i "${srcdir}"/0006-suitesparse-5.4.0-skip-building-GraphBLAS.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -rf "${srcdir}"/SuiteSparse-${pkgver} build-${CARCH}

  cd "${srcdir}"/build-${CARCH}

  # This avoids needing to use a very large patch (we still get diffs but
  # that is down to a bug in format string handling somewhere where-by an
  # extra 0-padding value is emitted in exponents of scientific floats).
  [[ -d "${PWD}"/bin ]] || mkdir "${PWD}"/bin
  echo "#!/usr/bin/env bash"                         > "${PWD}"/bin/diff
  echo "/usr/bin/diff --strip-trailing-cr \"\$@\""  >> "${PWD}"/bin/diff

  # This avoids needing to use another very large patch.
  [[ -d "${PWD}"/lib ]] || mkdir "${PWD}"/lib
  ar cru "${PWD}"/lib/librt.a

  PATH=${PWD}/bin:"${PATH}"          \
  CC=gcc                             \
  CXX=g++                            \
  CFLAGS="-DLONGBLAS='long long'"    \
  CXXFLAGS="-DLONGBLAS='long long'"  \
  LDFLAGS="-L${PWD}/lib"             \
  MY_METIS_LIB=-lmetis               \
  BLAS=-lopenblas                    \
  LAPACK=-lopenblas                  \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    make CMAKE_OPTIONS="-G\"MSYS Makefiles\" -DCMAKE_INSTALL_PREFIX=\"${MINGW_PREFIX}\" -DCMAKE_BUILD_TYPE=Release"
}

package() {
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,lib,include} || true

  cd "${srcdir}"/build-${CARCH}

  PATH=${PWD}/bin:"${PATH}"               \
  CC=gcc                                  \
  CXX=g++                                 \
  CFLAGS="-DLONGBLAS='long long'"         \
  CXXFLAGS="-DLONGBLAS='long long'"       \
  MY_METIS_LIB=-lmetis                    \
  BLAS=-lopenblas                         \
  LAPACK=-lopenblas                       \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  make install                          \
    INSTALL="${pkgdir}"${MINGW_PREFIX}  \
    DESTDIR="${pkgdir}"                 \
    CMAKE_OPTIONS="-G\"MSYS Makefiles\" -DCMAKE_INSTALL_PREFIX=\"${MINGW_PREFIX}\" -DCMAKE_BUILD_TYPE=Release"
}
